#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const ll N = 1e6 + 10, INF = 0x3f3f3f3f;

ll n;
ll w[N], sum[N], siz[N];
vector<ll> g[N];

void dfs1(ll x)
{
    siz[x] = 1;
    sum[x] = w[x];
    for (ll y : g[x]) {
        dfs1(y);
        siz[x] += siz[y];
        sum[x] += sum[y];
    }
}

ll cntd = 0;
ll dfs2(ll x)
{
    ++cntd;
    ll res = cntd * w[x];
    for (ll y : g[x]) {
        res += dfs2(y);
    }
    return res;
}

void solve()
{
    cin >> n;
    for (ll i = 1;i <= n;i++) cin >> w[i];
    for (ll i = 2;i <= n;i++) {
        ll fa; cin >> fa;
        g[fa].push_back(i);
    }
    dfs1(1);

    for (ll i = 1;i <= n;i++)
        sort(g[i].begin(), g[i].end(), [](ll a, ll b) {
        return siz[b] * sum[a] < siz[a] * sum[b];});

    cout << dfs2(1) << '\n';
}

signed main()
{
    //ios::sync_with_stdio(0);
    //cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    ll T = 1;
    //cin>>T;
    for (ll i = 1;i <= T;i++) {
        solve();
    }
}